package cn.ym.system.controller;

import cn.ym.basic.exception.BusinessException;
import cn.ym.basic.utils.AjaxResult;
import cn.ym.basic.utils.PageList;
import cn.ym.org.domain.Department;
import cn.ym.system.domain.Menu;
import cn.ym.system.query.MenuQuery;
import cn.ym.system.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：菜单
 */
@RestController
@RequestMapping("/menu")
@Api(value = "菜单接口" ,description = "处理菜单的所有请求")
public class MenuController {
    @Autowired
    private MenuService menuService;
    /**
     * 接口：添加或修改菜单信息
     */
    @ApiOperation(value = "添加或修改菜单信息")
    @PutMapping
    public AjaxResult save(@RequestBody Menu menu){

            if(menu.getId()==null){
                menuService.add(menu);
            }else {
                menuService.update(menu);
            }
            return new AjaxResult();


    }

    /**
     *接口：删除单个菜单信息
     */
    @ApiOperation(value = "删除单个菜单信息")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){

            menuService.delete(id);
            return new AjaxResult();

    }

    /**
     * 接口：查询所有菜单信息
     */
    @ApiOperation(value = "查询所有菜单信息")
    @GetMapping
    public List<Menu> findAll(){
        return menuService.findAll();
    }

    /**
     * 接口：查询单个菜单信息
     */
    @ApiOperation(value = "查询单个菜单信息")
    @GetMapping("/{id}")
    public Menu findOne(@PathVariable("id") Long id){
        return menuService.findOne(id);
    }


    /**
     * 接口：批量删除菜单信息
     */
    @ApiOperation(value = "批量删除菜单信息")
    @PatchMapping
    public AjaxResult patchdel(@RequestBody Long[]  ids){

            menuService.patchDel(ids);
            return new AjaxResult();

    }

    /**
     * 接口：分页查询 + 高级查询
     */
    @ApiOperation(value = "分页查询或高级查询")
    @PostMapping
    public PageList<Menu> queryPage(@RequestBody MenuQuery menuQuery){
        return menuService.queryPage(menuQuery);
   }


    /**
     * 接口：部门树
     */
    @ApiOperation(value = "部门树")
    @GetMapping("/menuTree")
    public List<Menu> menuTree(){
        return menuService.menuTree();
    }
}
